/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rhdtp.demopf.control.admin;

import com.rhdtp.demopf.model.dao.ClienteDAO;
import com.rhdtp.demopf.model.dao.PeliculaDAO;
import com.rhdtp.demopf.model.dao.PrestamoDAO;
import com.rhdtp.demopf.model.dto.Cliente;
import com.rhdtp.demopf.model.dto.Pelicula;
import com.rhdtp.demopf.model.dto.Prestamo;
import com.rhdtp.demopf.security.UserSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import org.apache.log4j.Logger;
import org.primefaces.context.RequestContext;

/**
 *
 * @author rhdtp
 */
public class Prestamos {

    private static final Logger logger = Logger.getLogger(Prestamos.class);
    private ClienteDAO clienteDAO;
    private PeliculaDAO peliculaDAO;
    private PrestamoDAO prestamoDAO;
    private UserSession userSession;
    private List<Prestamo> prestamos;
    private List<Prestamo> prestamosFilter;
    private Date fechaI;
    private Date fechaF;
    private Pelicula pelicula;

    public Prestamos() {
    }

    public void consulta() {
        prestamos = prestamoDAO.buscaFechaEntrega(fechaI, fechaF);
    }

    public void consulta2() {
        prestamos = prestamoDAO.buscaPelicula(pelicula.getId());
    }

    public List<Pelicula> completePelicula(String query) {
        List<Pelicula> allThemes = peliculaDAO.list();
        List<Pelicula> filteredThemes = new ArrayList<Pelicula>();

        for (int i = 0; i < allThemes.size(); i++) {
            Pelicula skin = allThemes.get(i);
            if (skin.getTitulo().toLowerCase().startsWith(query)) {
                filteredThemes.add(skin);
            }
        }

        return filteredThemes;
    }

    public void resetForm() {
        initObjects();
        RequestContext.getCurrentInstance().reset("registro:formulario");
        RequestContext.getCurrentInstance().reset("registro2:formulario2");
    }

    @PostConstruct
    public void initObjects() {
        pelicula = null;
        fechaF = null;
        fechaI = null;
    }

    public void msgInfo(String summary, String detail) {
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, summary, detail);
        FacesContext.getCurrentInstance().addMessage(null, message);
    }

    public void msgError(String summary, String detail) {
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, summary, detail);
        FacesContext.getCurrentInstance().addMessage(null, message);
    }

    public void msgWarning(String summary, String detail) {
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_WARN, summary, detail);
        FacesContext.getCurrentInstance().addMessage(null, message);
    }

    public List<Cliente> getClientes() {
        return clienteDAO.list();
    }

    /**
     * @param clienteDAO the clienteDAO to set
     */
    public void setClienteDAO(ClienteDAO clienteDAO) {
        this.clienteDAO = clienteDAO;
    }

    public UserSession getUserSession() {
        return userSession;
    }

    public void setUserSession(UserSession userSession) {
        this.userSession = userSession;
    }

    public PeliculaDAO getPeliculaDAO() {
        return peliculaDAO;
    }

    public void setPeliculaDAO(PeliculaDAO peliculaDAO) {
        this.peliculaDAO = peliculaDAO;
    }

    public PrestamoDAO getPrestamoDAO() {
        return prestamoDAO;
    }

    public void setPrestamoDAO(PrestamoDAO prestamoDAO) {
        this.prestamoDAO = prestamoDAO;
    }

    public Date getFechaI() {
        return fechaI;
    }

    public void setFechaI(Date fechaI) {
        this.fechaI = fechaI;
    }

    public Date getFechaF() {
        return fechaF;
    }

    public void setFechaF(Date fechaF) {
        this.fechaF = fechaF;
    }

    public List<Prestamo> getPrestamos() {
        return prestamos;
    }

    public void setPrestamos(List<Prestamo> prestamos) {
        this.prestamos = prestamos;
    }

    public List<Prestamo> getPrestamosFilter() {
        return prestamosFilter;
    }

    public void setPrestamosFilter(List<Prestamo> prestamosFilter) {
        this.prestamosFilter = prestamosFilter;
    }

    public Pelicula getPelicula() {
        return pelicula;
    }

    public void setPelicula(Pelicula pelicula) {
        this.pelicula = pelicula;
    }
}
